package cn.itsource.service.impl;

import cn.itsource.config.anno.MyLog;
import cn.itsource.domain.BlogArticleType;
import cn.itsource.domain.BlogFriendAddress;
import cn.itsource.domain.BlogParam;
import cn.itsource.mapper.ParamMapper;
import cn.itsource.query.BlogParamQuery;
import cn.itsource.service.BlogParamService;
import cn.itsource.util.UserTools;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service //交给spring容器管理
public class BlogParamServiceImpl implements BlogParamService {

    /**
     * 注入mapper层
     */
    @Autowired
    private ParamMapper mapper;

   @Override
    public PageInfo<BlogParam> pageList(BlogParamQuery paramQuery) {
       PageHelper.startPage(paramQuery.getPage(),paramQuery.getRows());
       //高级查询，封装查询条件 参数：查询的domain的class类型
       Example example = new Example(BlogParam.class);
       //判断keyword是有效值
       if (StringUtils.isNotBlank(paramQuery.getKeyword())){
           example.and().andLike("paramKey","%"+paramQuery.getKeyword()+"%");
           example.or().orLike("paramValue","%"+paramQuery.getKeyword()+"%");
       }
       List<BlogParam> blogParams = mapper.selectByExample(example);
       //封装成pageInfo
       PageInfo<BlogParam> blogParamPageInfo = new PageInfo<>(blogParams);
       //返回数据
       return blogParamPageInfo;

}

    @Override
    @MyLog(value = "参数列表")
    public BlogParam findOne(Long id) {

        //使用tk的方法查询一个
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BlogParam> findAll() {
//        Example example = new Example(BlogParam.class);
//        example.and().andEqualTo("status","1");
//        return mapper.selectByExample(example);
        //使用tk的方法查询全部
        return mapper.selectAll();
    }

    @Override
    @MyLog(value = "参数列表")
    public void saveOrUpdate(BlogParam param) {
        //        修改时间
        param.setUpdateTime(new Date());
//        修改人姓名
        param.setUpdateUserName(UserTools.getCurrentUserRealName());
        try {
            if (param.getId() == null) {
                mapper.insertSelective(param);
            } else {
                mapper.updateByPrimaryKey(param);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    @MyLog(value = "参数列表",isImportant = true)
    public void delete(Long id) {
        //根据主键id删除一个对象
        mapper.deleteByPrimaryKey(id);
    }

    }

